package com.mall.order.controller;

import com.mall.common.entity.Result;
import com.mall.order.entity.dto.CreateOrderDTO;
import com.mall.order.entity.po.Order;
import com.mall.order.entity.vo.OrderVO;
import com.mall.order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "订单服务")
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @Operation(summary = "创建订单")
    @PostMapping("/create")
    public Result<String> createOrder(@RequestBody CreateOrderDTO dto) {
        String order = orderService.createOrder(dto);
        return Result.success(order);
    }

    @Operation(summary = "根据id查询订单详情")
    @GetMapping("/{orderId}")
    public Result<Order> getOrder(@PathVariable String orderId) {
        Order order = orderService.getById(orderId);
        if (order == null) return Result.error("订单不存在");
        return Result.success(order);
    }

    @Operation(summary = "标记订单为已支付")
    @PutMapping("/{orderNo}")
    public Result<String> payOrder(@PathVariable("orderNo") Long orderNo) {
        boolean success  = orderService.markAsPaid(orderNo);
        if (success) {
            return Result.success("支付成功");
        } else {
            return Result.error("支付失败，订单不存在或状态异常");
        }
    }
}